package com.atguigu.listener;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyApplicationListener implements ServletContextListener, ServletContextAttributeListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //ServletContextListener.super.contextInitialized(sce);
        ServletContext application = sce.getServletContext();
        System.out.println(application.hashCode()+"应用域初始化");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //ServletContextListener.super.contextDestroyed(sce);
        ServletContext application = sce.getServletContext();
        System.out.println(application.hashCode()+"应用域销毁了");
    }

    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        //ServletContextAttributeListener.super.attributeAdded(scae);
        ServletContext application = scae.getServletContext();
        String key = scae.getName();
        Object value = scae.getValue();
        System.out.println(application.hashCode() + "应用域增加的数据 ："+ key + " = " + value);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        //ServletContextAttributeListener.super.attributeRemoved(scae);
        ServletContext application = scae.getServletContext();
        String key = scae.getName();
        Object value = scae.getValue();
        System.out.println(application.hashCode() + "应用域删除的数据 ："+ key + " = " + value);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        //ServletContextAttributeListener.super.attributeReplaced(scae);
        ServletContext application = scae.getServletContext();
        String key = scae.getName();
        Object value = scae.getValue();
        Object newValue = application.getAttribute(key);
        System.out.println(application.hashCode() + "应用域修改的数据 ："+ key + " = " + value + " -> " + newValue);
    }
}
